import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

import '../screens/home_screen.dart';
import '../screens/daily_must_see_screen.dart';
import '../screens/word_list_screen.dart';
import '../screens/add_edit_word_screen.dart';
import '../screens/card_memory_screen.dart';
import '../screens/practice_type_selection_screen.dart';
import '../screens/question_list_screen.dart';
import '../screens/add_edit_question_screen.dart';
import '../screens/question_practice_screen.dart';
import '../screens/my_profile_screen.dart';
import '../screens/check_in_screen.dart';
import '../screens/note_list_screen.dart';
import '../screens/add_edit_note_screen.dart';
import '../screens/essay_material_list_screen.dart';
import '../screens/add_edit_essay_material_screen.dart';
import '../screens/essay_material_detail_screen.dart';
import '../screens/ai_chat_screen.dart';
import '../screens/font_size_settings_screen.dart';
import '../screens/theme_settings_screen.dart';
import '../screens/backup_screen.dart';
import '../pages/database_check_page.dart';
import '../screens/debug_screen.dart';
import '../screens/question_import_export_screen.dart';
import '../screens/word_import_export_screen.dart';
import '../screens/note_essay_import_export_screen.dart';

import '../models/word.dart';

class AppRouter {
  late final GoRouter router;
  
  AppRouter() {
    router = GoRouter(
      initialLocation: '/',
      routes: [
        // 主页面 - 包含底部导航栏
        ShellRoute(
          builder: (context, state, child) => HomeScreen(child: child),
          routes: [
            // 每日必看
            GoRoute(
              path: '/',
              pageBuilder: (context, state) => const NoTransitionPage(
                child: DailyMustSeeScreen(),
              ),
            ),
            
            // 单词记忆
            GoRoute(
              path: '/word-list',
              pageBuilder: (context, state) => const NoTransitionPage(
                child: WordListScreen(),
              ),
              routes: [
                // 添加单词
                GoRoute(
                  path: 'add',
                  builder: (context, state) => const AddEditWordScreen(),
                  redirect: (context, state) {
                    // 如果是从添加页面返回，刷新数据
                    return null;
                  },
                ),
                // 编辑单词
                GoRoute(
                  path: 'edit/:wordId',
                  builder: (context, state) => AddEditWordScreen(
                    wordId: int.parse(state.pathParameters['wordId']!),
                  ),
                ),
                // 卡片记忆
                GoRoute(
                  path: 'card-memory',
                  builder: (context, state) => const CardMemoryScreen(),
                  redirect: (context, state) {
                    // 如果是从添加页面返回，刷新数据
                    return null;
                  },
                ),
                // 调试页面
                GoRoute(
                  path: 'debug',
                  builder: (context, state) => const DebugScreen(),
                ),
                // 数据库检查页面
                GoRoute(
                  path: 'db-check',
                  builder: (context, state) => const DatabaseCheckPage(),
                ),
              ],
            ),
            
            // 题目练习
            GoRoute(
              path: '/practice',
              pageBuilder: (context, state) => const NoTransitionPage(
                child: PracticeTypeSelectionScreen(),
              ),
              routes: [
                // 题目列表
                GoRoute(
                  path: 'questions/:questionType',
                  builder: (context, state) => QuestionListScreen(
                    questionType: state.pathParameters['questionType']!,
                  ),
                  routes: [
                    // 添加题目
                    GoRoute(
                      path: 'add',
                      builder: (context, state) => AddEditQuestionScreen(
                        questionType: state.pathParameters['questionType']!,
                      ),
                    ),
                    // 编辑题目
                    GoRoute(
                      path: 'edit/:questionId',
                      builder: (context, state) => AddEditQuestionScreen(
                        questionType: state.pathParameters['questionType']!,
                        questionId: int.parse(state.pathParameters['questionId']!),
                      ),
                    ),
                  ],
                ),
                // 题目练习
                GoRoute(
                  path: 'start/:questionType',
                  builder: (context, state) => QuestionPracticeScreen(
                    questionType: state.pathParameters['questionType']!,
                  ),
                ),
              ],
            ),
            
            // 我的
            GoRoute(
              path: '/profile',
              pageBuilder: (context, state) => const NoTransitionPage(
                child: MyProfileScreen(),
              ),
              routes: [
                // 签到
                GoRoute(
                  path: 'check-in',
                  builder: (context, state) => const CheckInScreen(),
                ),
                // 笔记列表
                GoRoute(
                  path: 'notes',
                  builder: (context, state) => const NoteListScreen(),
                  routes: [
                    // 添加笔记
                    GoRoute(
                      path: 'add',
                      builder: (context, state) => const AddEditNoteScreen(),
                    ),
                    // 编辑笔记
                    GoRoute(
                      path: 'edit/:noteId',
                      builder: (context, state) => AddEditNoteScreen(
                        noteId: int.parse(state.pathParameters['noteId']!),
                      ),
                    ),
                  ],
                ),
                // 作文素材列表
                GoRoute(
                  path: 'essay-materials',
                  builder: (context, state) => const EssayMaterialListScreen(),
                  routes: [
                    // 添加作文素材
                    GoRoute(
                      path: 'add',
                      builder: (context, state) => const AddEditEssayMaterialScreen(),
                    ),
                    // 编辑作文素材
                    GoRoute(
                      path: 'edit/:materialId',
                      builder: (context, state) => AddEditEssayMaterialScreen(
                        materialId: int.parse(state.pathParameters['materialId']!),
                      ),
                    ),
                    // 作文素材详情
                    GoRoute(
                      path: 'detail/:materialId',
                      builder: (context, state) => EssayMaterialDetailScreen(
                        materialId: int.parse(state.pathParameters['materialId']!),
                        fromTabIndex: int.tryParse(state.uri.queryParameters['fromTabIndex'] ?? '0'),
                      ),
                    ),
                  ],
                ),
                // AI聊天
                GoRoute(
                  path: 'ai-chat',
                  builder: (context, state) => const AIChatScreen(),
                ),
                // 字体大小设置
                GoRoute(
                  path: 'font-size-settings',
                  builder: (context, state) => const FontSizeSettingsScreen(),
                ),
                // 主题设置
                GoRoute(
                  path: 'theme-settings',
                  builder: (context, state) => const ThemeSettingsScreen(),
                ),
                // 我的备份
                GoRoute(
                  path: 'backup',
                  builder: (context, state) => const BackupScreen(),
                ),
                ],
            ),
            
            // 题目导入导出
            GoRoute(
              path: '/question-import-export',
              pageBuilder: (context, state) => const NoTransitionPage(
                child: QuestionImportExportScreen(),
              ),
            ),
            
            // 单词导入导出
            GoRoute(
              path: '/word-import-export',
              pageBuilder: (context, state) {
                final List<Word>? initialWords = state.extra as List<Word>?;
                return NoTransitionPage(
                  child: WordImportExportScreen(initialWords: initialWords),
                );
              },
            ),
            
            // 笔记和作文素材导入导出
            GoRoute(
              path: '/note_essay_import_export',
              pageBuilder: (context, state) => const NoTransitionPage(
                child: NoteEssayImportExportScreen(),
              ),
            ),
            

          ],
        ),
      ],
    );
  }
}